package com.keste.portal.ui.bean;

import com.keste.portal.ui.util.ADFUtils;

import com.keste.portal.ui.util.JSFUtils;

import java.util.ResourceBundle;

import javax.faces.event.ActionEvent;

import oracle.adf.controller.binding.BindingUtils;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.layout.RichPanelBox;
import oracle.adf.view.rich.component.rich.layout.RichPanelFormLayout;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.javatools.resourcebundle.BundleFactory;

import oracle.jbo.ViewObject;

public class DepartmentBean {
    private static final String CLASSNAME = DepartmentBean.class.getName();
    private static final ADFLogger LOGGER =
        ADFLogger.createADFLogger(CLASSNAME);
    ResourceBundle portalBundle =
        BundleFactory.getBundle("com.keste.portal.PortalBundle");
    private RichTable departmentTable;
    private RichPanelBox editPopup;
    private RichPanelFormLayout departmentPanelForm;
    private RichPopup dirtyCheckPopup;

    public DepartmentBean() {
    }

    public void deleteConfirmationDialogListener(DialogEvent dialogEvent) {
        // Add event code here...
        if (dialogEvent.getOutcome() == DialogEvent.Outcome.yes) {
            try {
                DCBindingContainer bindingContainer =
                    (DCBindingContainer)BindingUtils.getBindingContext().getCurrentBindingsEntry();
                DCIteratorBinding buDepartmentIterator =
                    bindingContainer.findIteratorBinding("DepartmentIterator");
                ViewObject viewObject =
                    bindingContainer.findIteratorBinding("DepartmentIterator").getViewObject();
                buDepartmentIterator.getCurrentRow().getAttribute("DepartmentName");
                buDepartmentIterator.getCurrentRow().setAttribute("IsDeleted",
                                                                  1);
                saveDepartment();
                viewObject.executeQuery();
                AdfFacesContext.getCurrentInstance().addPartialTarget(getDepartmentTable());
                AdfFacesContext.getCurrentInstance().addPartialTarget(getDepartmentPanelForm());
                JSFUtils.addFacesInformationMessage("Record has been deleted");
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.severe(e.getMessage());

            }
        }
    }

    private void saveDepartment() {
        try {
            OperationBinding operationBinding =
                ADFUtils.getOperationBinding("Commit");

            operationBinding.execute();
            operationBinding.getErrors().toArray();
            if (!operationBinding.getErrors().isEmpty()) {
                LOGGER.exiting(CLASSNAME, "saveDepartment");
                return;
            }
        } catch (Exception e) {
            LOGGER.severe(e.getMessage());
            e.printStackTrace();
        }
    }

    public void setDepartmentTable(RichTable departmentTable) {
        this.departmentTable = departmentTable;
    }

    public RichTable getDepartmentTable() {
        return departmentTable;
    }

    public void setEditPopup(RichPanelBox editPopup) {
        this.editPopup = editPopup;
    }

    public RichPanelBox getEditPopup() {
        return editPopup;
    }

    public void setDepartmentPanelForm(RichPanelFormLayout departmentPanelForm) {
        this.departmentPanelForm = departmentPanelForm;
    }

    public RichPanelFormLayout getDepartmentPanelForm() {
        return departmentPanelForm;
    }

    public void setDirtyCheckPopup(RichPopup dirtyCheckPopup) {
        this.dirtyCheckPopup = dirtyCheckPopup;
    }

    public RichPopup getDirtyCheckPopup() {
        return dirtyCheckPopup;
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void commitActionListener(ActionEvent actionEvent) {
        if (!(Boolean)ADFUtils.evaluateEL("#{controllerContext.currentViewPort.dataDirty}")) {
            JSFUtils.addFacesWarningMessage(portalBundle.getString("THERE_ARE_NO_CHANGES_TO_SAVE"));            
            return;
        }
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =
            bindings.getOperationBinding("Commit");
        operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return;
        }
        JSFUtils.addFacesInformationMessage(portalBundle.getString("ALL_CHANGES_ARE_SAVED"));        
    }

    /**
     * This method is called when cancel button is clicked in Department screen.
     * This method rolls back the transaction if data is dirty.
     * @return null
     */
    public void rollbackAction(ActionEvent actionEvent) {
        LOGGER.entering(CLASSNAME, "rollbackAction");
        LOGGER.info(actionEvent.toString());
        try {
            if (!(Boolean)ADFUtils.evaluateEL("#{controllerContext.currentViewPort.dataDirty}")) {
                JSFUtils.addFacesWarningMessage(portalBundle.getString("THERE_ARE_NO_CHANGES_TO_ROLLBACK"));
                LOGGER.exiting(CLASSNAME, "rollbackAction");
                return;
            }
            OperationBinding operationBinding =
                ADFUtils.getOperationBinding("Rollback");
            operationBinding.execute();
            if (!operationBinding.getErrors().isEmpty()) {
                LOGGER.exiting(CLASSNAME, "rollbackAction");
                return;
            }
            JSFUtils.addFacesInformationMessage(portalBundle.getString("ALL_CHANGES_ARE_REVERTED"));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.severe(e.getMessage());
            JSFUtils.addFacesErrorMessage(portalBundle.getString("INTERNAL_ERROR_CONTACT_SYSTEM_ADMINISTRATOR"));
        }
        LOGGER.exiting(CLASSNAME, "rollbackAction");
    }
}
